package com.company.array;

/**
 * @author jrue
 * @description https://leetcode-cn.com/problems/number-of-digit-one/
 * @date 2019/11/14 17:10
 * 给定一个整数 n，计算所有小于等于 n 的非负整数中数字 1 出现的个数。
 */
public class CountDigitOne {


    public static void main(String[] args) {
        System.out.println(countDigitOne(824883294));
    }

    public static int countOne(int n) {
        int num = 0;
        while (n!=0) {
            if (n %10 == 1) num++;
            n=n/10;
        }

        return num;
    }


    public static int countDigitOne(int n) {
        n++;
        int res = 0;
        for (int i = 1; i < n; i++) {
            res += countOne(i);
        }
        return res;
    }

}
